
#引入data函数

dbfile <- paste("db","data.r",sep="/")
source(dbfile)

getdata()

#feature engineering
# Grab title from passenger names
?gsub
full$Title <- gsub('(.*, )|(\\..*)', '', full$Name)

# Show title counts by sex
table(full$Sex, full$Title)

#将人数较少的一些分类分给miss mrs 和rare Title
# Titles with very low cell counts to be combined to "rare" level
rare_title <- c('Dona', 'Lady', 'the Countess','Capt', 'Col', 'Don', 
                'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer')

# Also reassign mlle, ms, and mme accordingly
full$Title[full$Title == 'Mlle']        <- 'Miss' 
full$Title[full$Title == 'Ms']          <- 'Miss'
full$Title[full$Title == 'Mme']         <- 'Mrs' 
full$Title[full$Title %in% rare_title]  <- 'Rare Title'

table(full$Sex,full$Title)

full$Surname <- sapply(full$Name, 
                       function(x){strsplit(x,split='[,.]')[[1]][1]})
# Create a family size variable including the passenger themselves
full$Fsize <- full$SibSp + full$Parch + 1
# Create a family variable 
full$Family <- paste(full$Surname, full$Fsize, sep='_')

#use ggplot2 to visualize the relationship between family size & survival
ggplot(full[1:891,], aes(x=Fsize, fill=factor(Survived))) +
  geom_bar(stat = 'count',position='dodge') +
  scale_x_continuous(breaks = c(1:11)) +
  labs(x="Family Size") +
  theme_few()

#since a large family size of more than 4 and singleton are more likely to perish
# Discretize family size
full$FsizeD[full$Fsize == 1] <- 'singleton'
full$FsizeD[full$Fsize < 5 & full$Fsize > 1] <- 'small'
full$FsizeD[full$Fsize > 4] <- 'large'

#show family size by survival using a mosaic plot!
mosaicplot(table(full$FsizeD,full$Survived),main="Family Size by Survival",shade=T)
# The first character is the deck. For example:
strsplit(full$Cabin[2], NULL)[[1]][1]
full$Deck<-factor(sapply(full$Cabin, function(x) strsplit(x, NULL)[[1]][1]))


embark_fare <- full %>%
  filter(PassengerId != 62 & PassengerId != 830)

#use ggplot2 to visualize embarkment, passenger class & median fare
ggplot(embark_fare,aes(x=Embarked, y=Fare, fill=factor(Pclass)))+
  geom_boxplot()+
  geom_hline(aes(yintercept=80),
             colour='red',
             linetype='dashed',
             lwd=2)+
  scale_y_continuous(labels = dollar_format())+
  theme_few()

# Since their fare was $80 for 1st class, they most likely embarked from 'C'
full$Embarked[c(62, 830)] <- 'C'

#Fare 是空的值
full[is.na(full$Fare),]
#这是一个从S登船的3等老年（60岁左右）男性
#查看跟他的登船地点相同，且等级相同的
ggplot(full[full$Pclass=='3' & full$Embarked=='S',],
       aes(x=Fare))+
  geom_density(fill="#99d6ff",alpha=.4)+
  geom_vline(aes(xintercept=median(Fare,na.rm=T)),
             colour='red',linetype='dashed',lwd=1)+
  scale_x_continuous(labels=dollar_format())+
  theme_few()
  
# Replace missing fare value with median fare for class/embarkment
full$Fare[1044] <- median(full[full$Pclass == '3' & full$Embarked == 'S', ]$Fare, na.rm = TRUE)
#年龄缺失记录数
sum(is.na(full$Age))

# Make variables factors into factors
factor_vars <- c('PassengerId','Pclass','Sex','Embarked',
                 'Title','Surname','Family','FsizeD')
?lapply#返回一个列表
full[factor_vars] <- lapply(full[factor_vars], function(x) as.factor(x))

# Set a random seed
set.seed(129)

# Perform mice imputation, excluding certain less-than-useful variables:
mice_mod <- mice(full[, !names(full) %in% c('PassengerId','Name','Ticket','Cabin','Family','Surname','Survived')], method='rf') 

mice_output <- complete(mice_mod)

#比较
# Plot age distributions
par(mfrow=c(1,2))
hist(full$Age, freq=F, main='Age: Original Data', 
     col='darkgreen', ylim=c(0,0.04))
hist(mice_output$Age, freq=F, main='Age: MICE Output', 
     col='lightgreen', ylim=c(0,0.04))

# Replace Age variable from the mice model.
full$Age <- mice_output$Age

# Show new number of missing Age values
sum(is.na(full$Age))
